package com.swak.fx.support.ui;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * UI 事件.
 * 
 * @author 618lf
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString
public class UIEvent extends Event {

	public static final EventType<UIEvent> UI = new EventType<>("UI.Event");
	private static final long serialVersionUID = 1L;

	/**
	 * 传递的指令
	 */
	private String cmd;

	/**
	 * 传递的数据
	 */
	private Object data;

	public UIEvent() {
		super(UI);
	}

	public static UIEvent of() {
		return new UIEvent();
	}
	
	// ================================================================================
	// 内置的一些事件转化器
	// ================================================================================

	public static UIEventTransmiter Window_Drag_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Drag);
	public static UIEventTransmiter Window_Fix_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Fix);
	public static UIEventTransmiter Window_Min_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Min);
	public static UIEventTransmiter Window_Max_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Max);
	public static UIEventTransmiter Window_Max2_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Max2);
	public static UIEventTransmiter Window_Close_Transmiter = new UIEventTransmiter(
			com.swak.fx.support.ui.UI.Events.Window_Close);
	
	static class UIEventTransmiter implements EventHandler<MouseEvent> {

		final com.swak.fx.support.ui.UI.Events event;

		public UIEventTransmiter(com.swak.fx.support.ui.UI.Events event) {
			this.event = event;
		}

		@Override
		public void handle(MouseEvent event) {
			Node node = (Node) event.getTarget();
			node.fireEvent(UIEvent.of().setCmd(this.event.getBindKey()));
		}
	}
}
